package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.dto.DishDto;
import com.itheima.pojo.Category;
import com.itheima.common.R;


import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.LogSource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {


   @Resource
   private DishFlavorService dishFlavorService;
   @Resource
    private DishService dishService;
   @Resource
   private CategoryService categoryService;


//    接口三：新增菜品功能开发
//    接口1：查询菜品分类列表数据
   @GetMapping("/list")
    public R<List<Category>> listSelect (Integer type ){

       LambdaQueryWrapper<Category> cw = new LambdaQueryWrapper<>();
       cw.eq( type !=null, Category::getType,type );
       cw.orderByDesc(Category::getSort);
       List<Category> list = categoryService.list(cw);
       return R.success(list);
   }


//新增菜品套餐分类
    @PostMapping
    public R<String> addCategory ( @RequestBody Category category){
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//
//      Employee id =(Employee) session.getAttribute("employee");
//        category.setCreateUser(id.getId());
//        category.setUpdateUser(id.getId());

        boolean save = categoryService.save(category);
        return save? R.success("ok"):R.error("no");
    }







//接口二：分类分页查询
    @GetMapping("/page")
    public R pageCategory (int page , int pageSize){

        IPage<Category> pageIUser =new Page<>(page, pageSize);
        LambdaQueryWrapper<IPage> pagees = new LambdaQueryWrapper<>();
        categoryService.page(pageIUser);
        long total = pageIUser.getTotal();
        List records = pageIUser.getRecords();

        Map pageMap = new HashMap();
        pageMap.put("records",records);
        pageMap.put("total",total);

        return R.success(pageMap);
    }


//接口三：删除分类
    @DeleteMapping
    public R deleteById( Long id ){

        boolean b = categoryService.deleteById(id);

        return b ? R.success("OK!OK!OK") : R.error("NO!NO!NO!") ;
    }
//接口四：修改分类
   @PutMapping
    public R update(@RequestBody Category category  ,HttpSession session ){
//       category.setCreateTime(LocalDateTime.now());
//       category.setUpdateTime(LocalDateTime.now());
//       Employee employee = (Employee) session.getAttribute("employee");
//       category.setCreateUser(employee.getId());
//       category.setUpdateUser(employee.getId());

    boolean b = categoryService.updateById(category);

    return b ? R.success("ok"):R.error("no");
}





//！！！！！！！！！Day05
//    接口1：查询分类表category表中所有的套餐分类： /category/list?type=2 【已完成】

//
//    接口2：查询分类表category表中所有菜品分类： /category/list?type=1 【已完成】
//
//    接口3：图片上传/下载接口 【已完成】


//    @GetMapping("/list")
//    public R<List<Category>> list(Category category){
//        //条件构造器
//        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
//        //添加条件
//        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
//        //添加排序条件
//        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
//
//        List<Category> list = categoryService.list(queryWrapper);
//        return R.success(list);
//    }
//





}
